package com.woniu.woniu_bx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.woniu_bx.pojo.Discuss;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DiscussMapper extends BaseMapper<Discuss> {
    //根据动态id查询评论和子评论
    @Select("select * from bx_discuss where moment_id = #{id} and pid is null order by time")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "subDiscuss",many=@Many(select="com.woniu.woniu_bx.mapper.DiscussMapper.findSubDiscussById")),
            @Result(column = "sender_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById")),
    })
    List<Discuss> findByMomentId();

    //查询子评论
    @Select("select * from bx_discuss where pid = #{pid} order by time")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "sender_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById")),
    })
    List<Discuss> findSubDiscussById(int pid);
    @Select("select count(*) from bx_discuss where moment_id = #{id}")
    int getDiscussNumByMomentId(Integer id);

    //一把查所有当前动态下的评论(其中子评论要包含父评论的信息目前需要id、name、content)
    @Select("select * from  bx_discuss where moment_id=#{id} order by floor")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "sender_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById")),
            @Result(column = "pid",property = "pdiscuss",one=@One(select = "com.woniu.woniu_bx.mapper.DiscussMapper.findByPid"))
    })
    List<Discuss> findAllDisscussByMomentId(int id);
    //查询父评论（前端评论组件需要）
    @Select("select * from bx_discuss where id = #{pid}")
    @Results({
            @Result(column = "sender_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById")),
    })
    Discuss findByPid(int pid);

    //查询floor最大值
    @Select("select max(floor) from bx_discuss")
    int findMaxFloor();

    //查询一条评论
    @Select("select * from bx_discuss where pid = 0 and floor=#{floor}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "sender_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById")),
    })
    Discuss findByFloor(int floor);

    //根据id查询数据
    @Select("select * from bx_discuss where id = #{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "sender_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById")),
            @Result(column = "pid",property = "pdiscuss",one=@One(select = "com.woniu.woniu_bx.mapper.DiscussMapper.findByPid"))
    })
    Discuss findById(int id);

    //新增二级评论
    @Insert("insert into bx_discuss values(default,#{content},#{senderId},#{momentId},#{pid},#{time},#{floor})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void add(Discuss discuss);
}
